package com.example.yrdev.ordering_system.Model;

import android.content.Context;

import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by luke on 30/12/2016.
 */

public class CardTypeInfo {
    public static final String DH_TABLE_NAME = "CardTypeInfo";


    public  static final String DH_Card_ID = "id";
    public  static final String DH_Card_Name = "name";
    public  static final String DH_Card_addRatio = "addRatio";

    public static final String DH_Card_Discount = "disount";//会员姓名


    public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " + DH_Card_ID + " TEXT," + DH_Card_Name + " TEXT,"
            + DH_Card_addRatio + " TEXT," + DH_Card_Discount + " TEXT"+  ")";



  private   static CardTypeInfo cardTypeInfo;

  private   ArrayList<CardTypeInfo>cardTypeInfoArrayList;

    public String id;
    public String addRatio;
    public String discount;
   public String name;

    public static CardTypeInfo getInstance(){

        if (cardTypeInfo==null)
            cardTypeInfo=new CardTypeInfo();

        return cardTypeInfo;

    }

    public ArrayList<CardTypeInfo> getCardTypeInfoArrayList() {


        return DingHanDB.getInstance(GlobalFunction.context).getCardTypeInfoList();

    }
   public static void deleteAllData(Context context){

       DingHanDB.getInstance(context).deleteAllData(DH_TABLE_NAME);
    }

    public void addCardTypeList(JSONObject object){
        if (cardTypeInfoArrayList==null)
            cardTypeInfoArrayList=new ArrayList<>();


        try {
            CardTypeInfo cardTypeInfo=new CardTypeInfo();
            cardTypeInfo.id=object.getString("id");
            cardTypeInfo.addRatio=object.getString("addRatio");
            cardTypeInfo.discount=object.getString("discount");
            cardTypeInfo.name=object.getString("name");
            cardTypeInfoArrayList.add(cardTypeInfo);

            DingHanDB.getInstance(GlobalFunction.context).insert(DH_TABLE_NAME,cardTypeInfo);

        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());
        }




    }


    public String getCorrespondCardTypeName(String id){

        String temp="";

        if (cardTypeInfoArrayList==null)
            cardTypeInfoArrayList=new ArrayList<>();


        if (cardTypeInfoArrayList.size()==0)
            cardTypeInfoArrayList= DingHanDB.getInstance(GlobalFunction.context).getCardTypeInfoList();

        Integer id2=Integer.parseInt(id,16);
        for (CardTypeInfo cardTypeInfo:cardTypeInfoArrayList){
            if (cardTypeInfo.id.equals(id2.toString())){

                temp=cardTypeInfo.name;

                break;

            }
        }

        return temp;

    }
}
